package com.sxbbc.common.core.service.impl;

import com.function.common.GetRest;
import com.function.common.RestResponse;
import com.sxbbc.common.basics.exceptions.ThrowJsonException;
import com.sxbbc.common.core.entity.jz.InfoAli;
import com.sxbbc.common.core.entity.jz.InfoWx;
import com.sxbbc.common.core.service.IInfoAliService;
import com.sxbbc.common.core.service.IPayService;
import com.sxbbc.common.core.service.IInfoWxService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 保存支付信息 实现类
 *
 * @author leehao
 * @date 2019-04-29 16:33
 */
@Service
public class PayServiceImpl implements IPayService {
    @Autowired
    private IInfoAliService infoAliService;
    @Autowired
    private IInfoWxService infoWxService;

    @Override
    public RestResponse editWxPayInfo(InfoWx wxInfos) {
        if (StringUtils.isBlank(wxInfos.getAppId())) {
            return GetRest.getFail("请输入微信APPID");
        } else if (StringUtils.isBlank(wxInfos.getWxNum())) {
            return GetRest.getFail("请输入微信号");
        } else if (StringUtils.isBlank(wxInfos.getMappId())) {
            return GetRest.getFail("请输入微信APP支付商户号");
        } else if (StringUtils.isBlank(wxInfos.getKeyval())) {
            return GetRest.getFail("请输入微信支付秘钥");
        } else if (StringUtils.isBlank(wxInfos.getMappId())) {
            return GetRest.getFail("请输入APP开放平台对应商户号APPID");
        } else if (StringUtils.isBlank(wxInfos.getMappsecretKey())) {
            return GetRest.getFail("请输入APP开放平台对应商户号secretkey");
        } else if (StringUtils.isBlank(wxInfos.getWxName())) {
            return GetRest.getFail("请输入微信公众号名称");
        } else if (StringUtils.isBlank(wxInfos.getReturnUrl())) {
            return GetRest.getFail("请输入微信支付网页回调");
        }
        InfoWx wxBefore = null;
        if (null != wxInfos.getWxNum()) {
            wxBefore = infoWxService.getById(wxInfos.getWxNum());
            if (wxBefore == null) {
                return GetRest.getFail("没有该信息，请刷新后重试");
            }
        }
        // 保存
        boolean wxSave = infoWxService.saveOrUpdate(wxInfos);
        if (!wxSave) {
            throw new ThrowJsonException("支付配置-微信支付保存失败");
        }
        return GetRest.getSuccess("保存微信配置信息成功");
    }

    @Override
    public RestResponse editAliPayInfo(InfoAli infoAli) {
        if (StringUtils.isBlank(infoAli.getAliPrivateKey())) {
            return GetRest.getFail("请上传app支付私钥");
        } else if (StringUtils.isBlank(infoAli.getAliPublicKey())) {
            return GetRest.getFail("请输入支付宝的公钥");
        } else if (StringUtils.isBlank(infoAli.getPartner())) {
            return GetRest.getFail("请输入支付宝商户号");
        } else if (StringUtils.isBlank(infoAli.getPartnerAccount())) {
            return GetRest.getFail("请输入支付宝商户账号");
        } else if (StringUtils.isBlank(infoAli.getPrivateKey())) {
            return GetRest.getFail("请输入支付宝商户的私钥");
        } else if (StringUtils.isBlank(infoAli.getRefundApp())) {
            return GetRest.getFail("请输入支付宝app退款编号");
        } else if (StringUtils.isBlank(infoAli.getRefundWeb())) {
            return GetRest.getFail("请输入支付宝web退款编号");
        } else if (StringUtils.isBlank(infoAli.getRefundPrivateKey())) {
            return GetRest.getFail("请输入支付宝退款私钥");
        } else if (StringUtils.isBlank(infoAli.getRefundPublicKey())) {
            return GetRest.getFail("请输入退款公钥");
        } else if (StringUtils.isBlank(infoAli.getReturnUrl())) {
            return GetRest.getFail("请输入支付宝回调域名");
        }
        InfoAli aliBefore = null;
        if (null != infoAli.getAliId()) {
            aliBefore = infoAliService.getById(infoAli.getAliId());
            if (aliBefore == null) {
                return GetRest.getFail("没有该信息，请刷新后重试");
            }
        }
        boolean aliSave = infoAliService.saveOrUpdate(infoAli);
        if (!aliSave) {
            throw new ThrowJsonException("支付配置-支付宝支付保存失败");
        }
        return GetRest.getSuccess("保存成功");
    }

    @Override
    public Map<String, Object> queryPayInfo() {
        Map<String, Object> map = new HashMap<>();
        List<InfoWx> wxList = infoWxService.list(null);
        if (!wxList.isEmpty() && wxList.size() > 0) {
            map.put("wxPay", wxList.get(0));
        }
        List<InfoAli> aliList = infoAliService.list(null);
        if (!aliList.isEmpty() && aliList.size() > 0) {
            map.put("aliPay", aliList.get(0));
        }
        return map;
    }
}
